//
//  LoginViewController.m
//  TCProject
//
//  Created by admin on 16/1/19.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "LoginViewController.h"
#import "User.h"
#import "AccountManager.h"
#import "BMBaseModel+BMExtensions.h"


@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;

@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
@property (weak, nonatomic) IBOutlet UIButton *loginAction;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backAction;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    // white.png图片自己下载个纯白色的色块，或者自己ps做一个
    [navigationBar setBackgroundImage:[UIImage imageWithColor:kGlobalColor]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[UIImage new]];
    
    NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:@"忘记密码？" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:12.f], NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}];
    [self.forgetButton setAttributedTitle:attrTitle forState:UIControlStateNormal];
   
}
- (IBAction)loginAction:(id)sender {
    NSString *username = _userName.text;
    if ([BMUtils isEmptyString:username]) {
        [self showHint:@"UID/手机号不能为空!"];
        return;
    }
    NSString *password = _password.text;
    if (password.length < 6 || password.length > 16) {
        [self showHint:@"请输入6-16位密码!"];
        return;
    }
    weakSelf(self);
    BMRequest *request = [BMRequest requestWithPath:@"login.htm" contentKey:kRoleType ? @"dispatcher" : @"owner"];
    request.params = @{@"username": username, @"password": password, @"role": kRole};
    [self showHudWithhint:kDefaultMessage];
    [User postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == 1) {
            [AccountManager sharedInstance].user = response.result;
           // ZDCreateOrderController *createVC = (id)self.presentingViewController;
            [self dismissViewControllerAnimated:YES completion:^{
//                if (weakSelf.type == 1) {
//                    [createVC sendAction:nil];
//                }
            }];
        } else {
            [self showHint:@"该账号或密码有误"];
        }
    }];

}

- (IBAction)backAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}



@end
